package com.cheng.example.atguigu.practice.proVscon.v1;

import java.util.LinkedList;


//synchronized
public class Test {

    private LinkedList list = new LinkedList();

    private int max = 10;

    public synchronized void pro(int i) {
        while (list.size() >=10){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        list.add(list);
        System.out.println("生产:"+i);
        notify();
    }

    public synchronized void con(int i) {
        while (list.size() <=0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        list.pop();
        System.out.println("消费:"+i);
        notify();
    }

    public static void main(String[] args) {
        Test test = new Test();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                test.con(i);
            }
        }).start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                test.pro(i);
            }
        }).start();

    }


}


